package com.ithiema.luence;

import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.NumericRangeQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
import org.junit.Test;
import org.wltea.analyzer.lucene.IKAnalyzer;

import java.io.File;
import java.io.IOException;

public class LuencetestDelete {
    /*
     * 演示：删除索引
     * 注意：
     * 	一般，为了进行精确删除，我们会根据唯一字段来删除。比如ID
     * 	如果是用Term删除，要求ID也必须是字符串类型！
     */

    @Test
    public void DeleteTest() throws IOException {
        //创建目录对象
        Directory directory = FSDirectory.open(new File("indexDir"));
        //创建配置对象
        IndexWriterConfig config = new IndexWriterConfig(Version.LATEST, new IKAnalyzer());
        //创建写出工具
        IndexWriter indexWriter = new IndexWriter(directory, config);

        //根据词条进行删除
//        indexWriter.deleteDocuments(new Term("id", "1"));
        //根据query对象删除，数值范围查询锁定一个具体id
//        Query query= NumericRangeQuery.newLongRange("id", 2L, 2L, true, true);
//        indexWriter.deleteDocuments(query);
        //删除全部
        indexWriter.deleteAll();
        indexWriter.commit();
        indexWriter.close();
    }


}
